<template>
  <div class="app-container">
    <div
      style="
        font-size: 19px;
        font-weight: 600;
        margin-bottom: 20px;
        margin-left: 20px;
      "
    >
      <div v-if="this.$route.params && this.$route.params.id">修改讲师</div>
      <div v-else>添加讲师</div>
    </div>

    <el-form
      label-width="120px"
      :model="teacher"
      :rules="rules"
      ref="teacherForm"
    >
      <el-form-item label="讲师名称" prop="name">
        <el-input v-model="teacher.name" />
      </el-form-item>
      <el-form-item label="讲师排序" prop="sort">
        <el-input-number v-model="teacher.sort" controls-position="right" />
      </el-form-item>
      <el-form-item label="讲师头衔" prop="level">
        <el-select v-model="teacher.level" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            value使用动态绑定的值，讲师头衔由1 2代替
          -->
          <el-option :value="1" label="高级讲师" />
          <el-option :value="2" label="首席讲师" />
        </el-select>
      </el-form-item>
      <el-form-item label="讲师资历" prop="career">
        <el-input v-model="teacher.career" />
      </el-form-item>
      <el-form-item label="讲师简介" prop="intro">
        <el-input v-model="teacher.intro" :rows="10" type="textarea" />
      </el-form-item>

       <!-- 讲师头像 -->
      <el-form-item label="讲师头像">

        <!-- 头衔缩略图 -->
        <pan-thumb :image="teacher.avatar"/>
        <!-- 文件上传按钮 -->
        <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
        </el-button>

        <!--
      v-show：是否显示上传组件
      :key：类似于id，如果一个页面多个图片上传控件，可以做区分
      :url：后台上传的url地址
      @close：关闭上传组件
      @crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow	"
          :width="300"
          :height="300"
          :key="imagecropperKey"
          :url="BASE_API+'/file/upload/avatar'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"/>


      </el-form-item>
      <br /><br /><br /> 

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from "@/api/edu/teacher";
//引入上传头像的功能组件
 import ImageCropper from '@/components/ImageCropper'
 import PanThumb from '@/components/PanThumb' 

export default {
  components: {ImageCropper,PanThumb}, //组件的声明
  data() {
    return {
      teacher: {
        name: "",
        sort: 0,
        leve: 1,
        career: "",
        intro: "",
        avatar: 
        "https://cloudavatar.oss-cn-beijing.aliyuncs.com/img-3b97642cce9bf66853ce0537c91b09a7.jpg",
      },//双向绑定
      imagecropperShow: false,//上传弹框组件是否显示
      imagecropperKey: 0,//上传组件唯一标识
      BASE_API: process.env.BASE_API,//获取dev.env.js里面的地址
      saveBtnDisabled: false,//保存按钮是都禁用
      rules: {
        //客户端验证规则
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { max: 5, message: "长度最大 5 个字符", trigger: "blur" },
        ],
        sort: [
          { type: "number", message: "排序必须为数字值" },
          { required: true, message: "请输入序号", trigger: "change" },
        ],
        career: [{ required: true, message: "请输入资历", trigger: "blur" }],
        intro: [{ required: true, message: "请输入简介", trigger: "blur" }],
        level: [{ required: true, message: "请选择头衔", trigger: "change" }],
      },
    };
  },
  created() {
    //调用
    this.init();
  },
  methods: {
       //保存按钮调用的方法
    saveOrUpdate() {
      this.$refs["teacherForm"].validate((valid) => {
        if (valid) {
          //判断修改或添加 teacher是否有id
          if (!this.teacher.id) {
            //添加
            this.addTeacher();
          } else {
            //修改
            this.updateTeacher();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //添加讲师的方法
    addTeacher() {
      teacherApi.addTeacher(this.teacher).then((response) => {
        //添加成功
        //提示成功
        this.$message({
          type: "success",
          message: "添加成功！",
        });
        //回到讲师列表页面
        //vue路由跳转
        this.$router.push({
          path: "/teacher",
        });
      });
    },
init() {
      //判断路径有id值  修改操作
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.getInfo(id);
      } else {
        //判断路径没有id值  添加操作
        //清空表单即清空teacher
        this.teacher = {
          name: "",
          sort: "",
          level: "",
          career: "",
          intro: "",
          avatar: "https://cloudavatar.oss-cn-beijing.aliyuncs.com/img-3b97642cce9bf66853ce0537c91b09a7.jpg",
        }; //v-model双向绑定
      }
    },
    //根据讲师id查到讲师信息 回显操作
    getInfo(id) {
      teacherApi
        .getTeacherInfo(id)
        .then((response) => {
          console.log(response);
          this.teacher = response.data;
        })
        .catch((response) => {
          this.$message({
            type: "error",
            message: "获取数据失败",
          });
        });
    },
//修改讲师的方法
    updateTeacher() {
      teacherApi.updateTeacher(this.teacher).then((response) => {
        //修改成功
        //提示成功
        this.$message({
          type: "success",
          message: "修改成功！",
        });
        //回到讲师列表页面
        //vue路由跳转
        this.$router.push({
          path: "/teacher",
        });
      });
    },
    
     // 上传成功后的回调函数
    cropSuccess(data) {
      console.log(data)
      this.imagecropperShow = false
      this.teacher.avatar = data.ossUrl
      // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },

    // 关闭上传组件
    close() {
      // 关闭了上传图片框
      this.imagecropperShow = false
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    }
  },
};
</script>
